package com.cnu.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnu.sys.entity.Regular;
import com.cnu.sys.mapper.RegularMapper;
import com.cnu.sys.service.IRegularService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lmq
 * @since 2023-09-06
 */
@Service
public class RegularServiceImpl extends ServiceImpl<RegularMapper, Regular> implements IRegularService {

    @Override
    public List<Map<String, Object>> getList() {
        List<Regular> list = this.list();
        List<Map<String, Object>> rows = new ArrayList<>();

        for(Regular regular: list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", regular.getId());
            map.put("longitude", regular.getLongitude());
            map.put("latitude", regular.getLatitude());
            rows.add(map);
        }
        return rows;
    }

    @Override
    public List<Map<String, Object>> computeTwo() {
        List<Regular> list = this.list();
        List<Map<String, Object>> rows = new ArrayList<>();
        Map<String, Object> result = new HashMap<>();

        // 作业量统计
        if(!list.isEmpty()) {
            Regular firstRegular = list.get(0);

            String firstGpsTime = firstRegular.getGpsTime();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            double res = 0;

            try {
                long millSum = 0;
                Date firstDate = dateFormat.parse(firstGpsTime);
                for(Regular regular: list) {
                    Date dateTmp = dateFormat.parse(regular.getGpsTime());
                    millSum += (dateTmp.getTime() - firstDate.getTime());
                    firstDate = dateTmp;
                }
                res = (double) millSum / 60000;
                result.put("sumTime", res);
            } catch (Exception e) {
                // 处理日期解析异常
                e.printStackTrace();
            }
        }

        // 有效作业时间
        if(!list.isEmpty()) {
            Regular firstRegular = list.get(0);

            String firstGpsTime = firstRegular.getGpsTime();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            double res = 0;

            try {
                long millSum = 0;
                Date firstDate = dateFormat.parse(firstGpsTime);
                for(Regular regular: list) {
                    Date dateTmp = dateFormat.parse(regular.getGpsTime());
                    if(regular.getStatus().equals("TRUE")) {
                        millSum += (dateTmp.getTime() - firstDate.getTime());
                    }
                    firstDate = dateTmp;
                }
                res = (double) millSum / 60000;
                result.put("sumTimeReal", res);
            } catch (Exception e) {
                // 处理日期解析异常
                e.printStackTrace();
            }
        }

        // 作业平均速度
        if(!list.isEmpty()) {
            int cnt = 0;
            double res = 0;
            for(Regular regular: list) {
                if(regular.getStatus().equals("TRUE")) {
                    res += regular.getSpeed();
                    cnt++;
                }
            }
            res /= cnt;
            result.put("avgSpeed", res);
        }

        // 转弯平均速度
        if(!list.isEmpty()) {
            int cnt = 0;
            double res = 0;
            Regular first = list.get(0);
            for(Regular regular: list) {
                double tmp = (regular.getCourse() - first.getCourse() + 360) % 360;
                if(tmp >= 20 && tmp <= 70) {
                    res += regular.getSpeed();
                    cnt++;
                }
                first = regular;
            }
            res /= cnt;
            result.put("avgReSpeed", res);
        }

        // 作业面积
        if(!list.isEmpty()) {
            double res = 0;

            int i = 0;
            while(i + 1 < list.size()) {
                if(list.get(i).getStatus().equals("TRUE") && list.get(i + 1).getStatus().equals("TRUE")) {
                    double x2 = (list.get(i + 1).getX() - list.get(i).getX()) * (list.get(i + 1).getX() - list.get(i).getX());
                    double y2 = (list.get(i + 1).getY() - list.get(i).getY()) * (list.get(i + 1).getY() - list.get(i).getY());
                    res += Math.sqrt(x2 + y2) * list.get(i).getWidth();
                }
                i++;
            }

            result.put("area", res);
        }

        rows.add(result);
        return rows;
    }

    @Override
    public List<Map<String, Object>> computeThree() {

        List<Regular> list = this.list();
        List<Map<String, Object>> rows = new ArrayList<>();
        Map<String, Object> result = new HashMap<>();

        // 时间利用率
        if(!list.isEmpty()) {
            Regular firstRegular = list.get(0);

            String firstGpsTime = firstRegular.getGpsTime();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            double res = 0;

            try {
                long millSum = 0, millSumReal = 0;
                Date firstDate = dateFormat.parse(firstGpsTime);
                for(Regular regular: list) {
                    Date dateTmp = dateFormat.parse(regular.getGpsTime());
                    millSum += (dateTmp.getTime() - firstDate.getTime());
                    if(regular.getStatus().equals("TRUE")) {
                        millSumReal += (dateTmp.getTime() - firstDate.getTime());
                    }
                    firstDate = dateTmp;
                }
                res = (double) millSumReal / millSum;
                result.put("timeRate", res * 100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 工作行程率
        if(!list.isEmpty()) {
            double sum = 0, sumReal = 0;

            int i = 0;
            while(i + 1 < list.size()) {
                double x2 = (list.get(i + 1).getX() - list.get(i).getX()) * (list.get(i + 1).getX() - list.get(i).getX());
                double y2 = (list.get(i + 1).getY() - list.get(i).getY()) * (list.get(i + 1).getY() - list.get(i).getY());
                if(list.get(i).getStatus().equals("TRUE") && list.get(i + 1).getStatus().equals("TRUE")) {
                    sumReal += Math.sqrt(x2 + y2);
                }
                sum += Math.sqrt(x2 + y2);
                i++;
            }
            double res = sumReal / sum * 100;
            result.put("workRate", res);
        }

        if(!list.isEmpty()) {
            int cnt = 0, sum = 0;
            for(Regular regular: list) {
                if(regular.getDepth() >= regular.getStandard()) {
                    cnt++;
                }
                sum++;
            }
            double res = (double)cnt / sum * 100;
            result.put("passRate", res);
        }
        rows.add(result);
        return rows;
    }
}
